﻿using System;
using System.Collections.Generic;

namespace CPAIS.Model
{
    /// <summary>
    /// Represents a clinical action event.
    /// </summary>
    public class ActionEvent : ClinicalEvent
    {
        /// <summary>
        /// Constructor with event occurred day and type name.
        /// </summary>
        /// <param name="owner">Owner trace</param>
        /// <param name="day">Day of the event occurred</param>
        /// <param name="typeName">Action type name</param>
        public ActionEvent(Trace owner, int day, string typeName)
            : base(owner, day)
        {
            // if (string.IsNullOrEmpty(typeName))
            //     throw new ArgumentNullException("typeName");
            typeIndex = owner.Owner.AddActionType(typeName);
        }

        /// <summary>
        /// Constructor with event occurred timestamp and type name.
        /// </summary>
        /// <param name="owner">Owner trace</param>
        /// <param name="timestamp">Timestamp of the event occurred</param>
        /// <param name="typeName">Action type name</param>
        public ActionEvent(Trace owner, DateTime timestamp, string typeName)
            : base(owner, timestamp)
        {
            // if (string.IsNullOrEmpty(typeName))
            //     throw new ArgumentNullException("typeName");
            typeIndex = owner.Owner.AddActionType(typeName);
        }

        /// <summary>
        /// Gets the event type name.
        /// </summary>
        public override string TypeName
        {
            get { return owner.Owner.GetActionType(typeIndex); }
        }

        /// <summary>
        /// Sorts the events in ASCENDING order by timestamp. If days is
        /// not specified, set the day of the event.
        /// </summary>
        /// <param name="events">Clinical event list</param>
        public static void OrderEvents(List<ActionEvent> events)
        {
            if (events != null && events.Count > 1)
            {
                events.Sort();
                if (events[0].IsTimestampAvailable)
                {
                    foreach (var ev in events)
                    {
                        ev.day = (int)(ev.timestamp - ev.owner.Admit).TotalDays;
                    }
                }
            }
        }
    }
}